/**
 * 数组-有序数组的平方-双指针法-力扣977
 */

public class Practice_26 {
    public static void main(String[] args) {
        int[] nums = {-5,2,3,4};

        int[] r = sortedSquares(nums);

    }

    public static int[] sortedSquares(int[] nums) {

        int k = nums.length - 1;
        int[] result = new int[k+1];


        if (nums == null || nums.length == 0) {
            return null;
        } else {
            int l = 0;
            int r = nums.length - 1;

            while (l <= r) {
                if (nums[l] * nums[l] > nums[r] * nums[r]) {
                    result[k] = nums[l] * nums[l];

                    l = l + 1;
                } else {
                    result[k] = nums[r] * nums[r];
                    r = r - 1;

                }
                k = k - 1;

            }
        }

        return result;
    }

}
